<template>
  <div class="app-container">
    <!-- :accordion="true" 每次只同时打开一个节点 -->
    <!-- :default-expanded-keys="idArr" -->
    <el-tree
      :data="TreeData"
      :props="defaultProps"
      :default-expand-all="false"
      :accordion="true"
      :expand-on-click-node="false"
    >
      <!-- 定义节点的结构 -->
      <template v-slot="{ data }">
        <el-row
          type="flex"
          align="middle"
          justify="space-between"
          style="width: 100%; height: 45px"
        >
          <el-col>{{ data.name }}</el-col>
          <el-col :span="6">
            <div class="lili">
              <span class="conservator">{{ data.managerName }}</span>
              <el-dropdown @command="handleCommand($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    command="添加子部门"
                  >添加子部门</el-dropdown-item>
                  <el-dropdown-item
                    command="编辑部门"
                  >编辑部门</el-dropdown-item>
                  <el-dropdown-item command="删除">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-tree>
    <!-- 放置弹层组件 -->
    <!-- eslint-disable-next-line vue/attribute-hyphenation -->
    <AddDept
      ref="SonMethod"
      :title="title"
      :current-i-d="CurrentID"
      :show-dialog.sync="showDialog"
      @updateDepartment="getDepartment"
    />
  </div>
</template>

<script>
import { deleteDepartment, getDepartment } from '@/api/department/index'
import { transListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'

export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      // idArr: [], // 默认展开标题的数组
      CurrentID: null, // 添加部门的ID
      TreeData: [],
      showDialog: false,
      title: '',
      defaultProps: {
        children: 'children', // 定义子树状结构
        label: 'name' // 树状结构名称,name和接口文档文字相同
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      // 获取部门数据，渲染更新
      const res = await getDepartment()
      this.TreeData = transListToTreeData(res.data.data, 0)
      // console.log(res.data.data)
      // res.data.data.forEach(ele => {
      //   this.idArr.push(ele.name)
      // })
      // console.log(this.idArr)
    },
    async handleCommand(command, id) {
      if (command === '添加子部门') {
        this.showDialog = true // 点击，显示弹层组件
        this.CurrentID = id // 点击后存储当前点击部门的id
        this.title = '添加子部门'
      }
      if (command === '编辑部门') {
        this.showDialog = true // 点击，显示弹层组件
        this.CurrentID = id // 点击后存储当前点击部门的id
        this.title = '编辑部门'
        // 通过$refs调用子组件的方法，由于是同步任务，需要等待异步获取id后执行
        this.$nextTick(() => {
          this.$refs.SonMethod.getDepartmentDetail() // this.$refs.SonMethod 相当于组件的this
        })
      }
      if (command === '删除') {
        await this.$confirm('确定删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          // 发送请求 完成删除
        })
        await deleteDepartment(id)
        this.$message.success('删除成功')
        this.getDepartment()
      }
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 40px 60px;
}
.conservator {
  padding-right: 20px;
}
.lili{
  display: flex;
  justify-content: space-between;
}
</style>
